package com.sunhao.myapplication.util;

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Build;

import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

public class PermissionUtil {


    //    检查多个权限。返回true表示已完全开启，false表示未完全启用
    public static boolean checkPermissions(Activity act, String[] permissions, int requestCode) {
        //判断安卓版本是否大于6.0
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            int check = PackageManager.PERMISSION_GRANTED;//默认值为授权
            for (String permission : permissions) {
                check = ContextCompat.checkSelfPermission(act, permission);
                //只要有一个没有授权,结束循环
                if (check != PackageManager.PERMISSION_GRANTED) {
                    break;
                }
            }
            //如果没有授权，给用户弹出弹窗,请求权限
            if (check != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(act, permissions, requestCode);
                return false;
            }
        }
        return true;
    }

    public static boolean checkGrant(int[] grantResults) {
        if (grantResults != null) {
            for (int result : grantResults) {
                if (result != PackageManager.PERMISSION_GRANTED) {
                    return false;
                }
            }
            return true;
        }
        return false;
    }
}
